package com.base.api.redis;

import com.base.self.entity.PtCurrent;
import com.base.self.entity.PtOut;
import com.base.self.service.ICommodityService;
import com.base.self.service.IPtCurrentService;
import com.base.self.service.IPtOutService;
import com.base.self.service.ISelfOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 主要作用就是:接收过期的redis消息,获取到key,key就是订单号
 */
@Slf4j
@Transactional
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private final IPtCurrentService iPtCurrentService;
    private final IPtOutService iPtOutService;
    private final ISelfOrderService iSelfOrderService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer,
                                      IPtCurrentService iPtCurrentService,
                                      IPtOutService iPtOutService,
                                      ISelfOrderService iSelfOrderService) {
        super(listenerContainer);
        this.iPtCurrentService = iPtCurrentService;
        this.iPtOutService = iPtOutService;
        this.iSelfOrderService = iSelfOrderService;
    }

//    public static void main(String[] args) {
//        String orderCode = "ptCodde:21";
//        String[] strings = orderCode.split("ptCode:");
//        System.out.println(strings[0]);
//        System.out.println(strings[1]);
//    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        String orderCode = message.toString();
        log.warn("[redis监听过期键] {}", orderCode);
        if (!StringUtils.isBlank(orderCode) && StringUtils.contains(orderCode, "ptCode")) {
            String[] strings = orderCode.split("ptCode:");
            if (strings.length > 1) {
                try {
                    Integer id=Integer.parseInt(strings[1]);
                    log.warn("[拼团失败通知] orderCode={}", id);
                    PtCurrent ptCurrent = iPtCurrentService.getBaseMapper().selectById(id);
                    log.warn("[拼团失败通知] result={}", ptCurrent.toString());
                    iPtCurrentService.getBaseMapper().deleteById(id);
                    log.warn("[拼团失败通知] 删除记录{}", id);
                    PtOut pt=new PtOut(null,ptCurrent.getSub1(),ptCurrent.getSub2(),ptCurrent.getSub3(),ptCurrent.getSub4(),ptCurrent.getSid(),ptCurrent.getCreateTime(),new Date());
                    iPtOutService.getBaseMapper().insert(pt);
                    log.warn("[拼团失败通知] 插入拼团失败{}", pt.toString());
                    iSelfOrderService.partakerFailing(pt.getId(), ptCurrent.getOrderNo());
                    log.warn("拼团失败处理完毕");
                } catch (Exception e) {
                    log.warn("[拼团失败报错] {}",e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }
}
